const {
  myApi
} = require('../../../../api/index')
const app = getApp()
import utf8Array2Str from 'utf8array2str';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    IMGBASEURL: app.globalData.IMGBASEURL,
    userList: [],
    sum: 0,
    showH: false,
    key: "",
    id: ""
  },

  onClose() {
    this.setData({
      showH: false
    })
  },
  // 点击生成海报
  generatePoster() {
    let thet = this
    wx.showLoading({
      title: '加载海报中',
    })
  
      myApi.createposter({
        CreateParams: {
          UserId: wx.getStorageSync('userInfo').UserId,
          ActivityId: thet.data.id,
        },
        img:"https://staticimg.aiiiin.com/wxapp/playbillHb.png",
        ActivityUrl: "pages/my/Inviting-red-envelopes/index",
        CodeType: 1
      }).then((res) => {
        
          if(res.Result){
            wx.downloadFile({
              url: res.Result,
              success: (res) => {
                wx.hideLoading()
                wx.showShareImageMenu({
                  path: res.tempFilePath
                })
              }
            })
             
          }
         
      })

  },
  onerror(e){
    let index = e.currentTarget.dataset.index
     let  list  =  this.data.userList
       list[index].Avatar = this.data.IMGBASEURL + "user_64.png"
      this.setData({
        userList:list
      }) 
  },
  /**
   * 生命周期函数--监听页面加载
   */
  init(e) {
    let thet = this
    myApi.getsercetkey({
      CreateParams: {
        UserId: wx.getStorageSync('userInfo').UserId,
        ActivityId: thet.data.id
      }
    }).then((res) => {
      thet.setData({
        key: res.Result.SercetKey
      })
      myApi.getinvitefriendlistbyuserid({
        Key: res.Result.SercetKey
      }).then((res) => {
        thet.setData({
          userList: res.Result.Data,
          sum: res.Result.TotolMoney
        })
      })

    })
  },
  onLoad(options) {
    this.setData({
      id: options.id
    })
    if (options.isDraw) {
      this.setData({
        showH: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

   this.init()
  },


  onShareAppMessage(e) {
    return {
      title: '邀好友 领好礼',
      path: 'pages/my/Inviting-red-envelopes/index?key='+ this.data.key,
      imageUrl: this.data.IMGBASEURL + 'yaoqinghaoyou.png',
      success: function (res) {
        // 分享成功的回调  

      }
    }
  }


})